package com.z.admin.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.z.admin.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author z
 * @since 2022-02-13
 */
public interface RoleMapper extends BaseMapper<Role> {

    @Select("SELECT permission FROM t_permission p LEFT JOIN t_role_permission rp on p.id=rp.permission_id LEFT JOIN t_user_role ur on rp.role_id=ur.role_id where ur.user_id=#{userId}")
    List<String> selectPermissionByUserId(Integer userId);

    @Select("select * from t_role t where t.name=#{name}")
    Role getRole(String name);

    @Select("select * from t_role t where t.id=#{id}")
    Role findRoleById(Long id);

    @Select("select * from t_role t")
    List<Role> findAll();

    int updateRole(Role user);

    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    @Insert("insert into t_role(name,description) values(#{name},#{description})")
    Integer save(Role user);

    @Delete("delete from t_role where id=#{id}")
    void delete(Long id);

    @Select("select count(*) from t_role")
    Integer conutAllRoles();

    @Select("select * from t_role t order by t.id limit #{startPosition} , #{limit}")
    List<Role> findRoleByPage(@Param("startPosition") Integer startPosition, @Param("limit") Integer limit);

    @Select("select count(*) from t_role ${whereSql}")
    Integer conutByMap(String whereSql);

    @Select("select * from t_role t ${where} order by t.id limit #{startPosition} , #{limit}")
    List<Role> findPageByWhereSql(String where, Integer startPosition, Integer limit);

    @Select("select * from t_role t where t.name=#{name}")
    Role findRoleByName(String name);
}
